package com.homedo.microservice.open.service.gateway.manager.web.service;

import com.google.common.collect.Lists;
import com.homedo.common.bean.vo.context.UserContext;
import com.homedo.common.dao.bean.po.base.BasePo;
import com.homedo.common.service.BaseService;
import com.homedo.common.util.Detect;
import com.homedo.microservice.open.service.gateway.manager.web.bean.enums.MarkEnum;
import com.homedo.microservice.open.service.gateway.manager.web.bean.po.User;
import com.homedo.microservice.open.service.gateway.manager.web.bean.po.enhanced.UserEnhanced;
import com.homedo.microservice.open.service.gateway.manager.web.persistence.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 作者 jgshun
 * @date 创建时间 2018年10月19日 11时25分30秒
 */
@Service
public class UserService extends BaseService<User> {
    @Autowired
    private UserDao userDao;


    @Override
    public List<UserEnhanced> getEnhanceds(List<?> ids, UserContext userContext) {
        List<User> users = userDao.getListByIds(ids, User.class);

        if (!Detect.notEmpty(users)) {
            return null;
        }

        return this.convent2Enhanceds(users);
    }


    @Override
    public List<UserEnhanced> getEnhanceds4Active(List<?> ids, UserContext userContext) {
        List<User> users = userDao.getListByIdsGreaterThanMark0(ids, User.class);

        if (!Detect.notEmpty(users)) {
            return null;
        }

        return this.convent2Enhanceds(users);
    }


    @Override
    public List<UserEnhanced> convent2Enhanceds(List<? extends BasePo> pos) {
        List<User> users = (List<User>) pos;
        List<UserEnhanced> userEnhanceds = Lists.newArrayList();
        users.forEach(e -> userEnhanceds.add(new UserEnhanced(e)));
        return userEnhanceds;
    }


    public UserEnhanced getEnhancedByUsername(String username, UserContext userContext) {
        User user = new User();
        user.setUsername(username);
        user.setMark((int) MarkEnum.NO_DELETE.getCode());
        user = userDao.getOneByRecord(user);

        if (user == null) {
            return null;
        }
        return this.convent2Enhanced(user);
    }
}
